using UnityEngine;

public class Player : MonoBehaviour
{
    float h;
    int combo = 1;
    bool isAttack = false;
    public bool isRight = true;
    [Header("Player Move")]
    public Rigidbody2D rb;
    private float xInput;
    public float moveSpeed = 5f;

    public float speed = 10f;
    public SpriteRenderer sr;
    public LayerMask groundLayer;
    public AudioSource au;
    public AudioClip attackClip;
    //
    public Transform attackRightPos;
    public Transform attackLeftPos;
    public GameObject attackTrigger;

    public float jumpSpeed = 10f;
    public Animator animator;
    public bool isGround = false;
    void Start()
    {
        Application.targetFrameRate = 60;
    }

    void Update()
    {
        //检测是否在地面上
        CheckGround();

        //移动
        //Move();

        //攻击
        Attack();

        PlayerMove();
        //按下空格键输出调试信息
        if (Input.GetKeyDown(KeyCode.Space))
        {
            Debug.Log("空格键被按下");
        }
    }
    void FixedUpdate()
    {
        PlayerMoveFix();//玩家移动物理
    }
    public void PlayerMove()
    {
        xInput = Input.GetAxisRaw("Horizontal");//获取轴输入
        if (xInput != 0 && animator.GetBool("IsRun")==false)
        {
           //（角色翻转）
            //播放动画
            animator.SetBool("IsRun", true);//设置玩家动画状态
            if (xInput > 0 && isRight == false) //判断向右移动
            {
                isRight =true;
                sr.flipX = false;//取消翻转
                
            }
            
            else if (xInput < 0 && isRight == true)//判断向左移动
            {
                isRight = false;
                sr.flipX = true;//翻转
            }
        }
        
        else if (!(xInput != 0 )&& animator.GetBool("IsRun") == true)//判断停止移动
        {
            animator.SetBool("IsRun", false);
        }
    }
    //玩家移动
    public void PlayerMoveFix()
    {
        rb.linearVelocity = new Vector2(xInput * moveSpeed, rb.linearVelocityY);
    }
    public void CheckGround()
    {
        //检测是否在地面上
        Vector2 startPos = transform.position;
        Vector2 endPos = transform.position + Vector3.down * 1f;
        RaycastHit2D hit = Physics2D.Linecast(startPos, endPos, groundLayer);
        if (hit.collider != null)
        {
            //空中
            isGround = true;
            animator.SetBool("IsJump", false);
        }
        else
        {
            //地板之上
            isGround = false;
            animator.SetBool("IsJump", true);
        }
    }

    public void Move()
    {
        //GetKeyDown、GetKey、GetKeyup
        //获取输入轴 移动
        if (!isAttack)
        {
            h = Input.GetAxisRaw("Horizontal");
        }
        else
        {
            h = 0;
        }
        rb.linearVelocityX = h * speed;

        //移动动画
        if (h > 0)
        {
            animator.SetBool("IsRun", true);
            sr.flipX = false;

        }
        else if (h < 0)
        {
            animator.SetBool("IsRun", true);
            sr.flipX = true;
        }
        else
        {
            animator.SetBool("IsRun", false);
        }

        //跳跃
        if (Input.GetKeyDown(KeyCode.Space))
        {
            if (isGround)
            {
                rb.linearVelocityY = jumpSpeed;
            }
        }
    }

    //攻击
    public void Attack()
    {
        if (!Input.GetKeyDown(KeyCode.J)){ return; }//判断是否摁下攻击键
        if (!isGround) { return; } //判断是否在地面上
        if (isAttack) { return; } //判断是否在攻击

        CancelInvoke(nameof(AttackEnd));//取消攻击结束的延迟
        CancelInvoke(nameof(AttackAnimEnd));//取消攻击动画结束的延迟
        CancelInvoke(nameof(AttackComboReset));//取消攻击连击重置的延迟

        isAttack = true;//设置攻击状态
        //攻击动画
        if (combo == 1)
        {
            animator.SetTrigger("Attack1");
            combo = 2;
        }
        else if (combo == 2)
        {
            animator.SetTrigger("Attack2");
            combo = 3;
        }
        else if (combo == 3)
        {
            animator.SetTrigger("Attack3");
            combo = 1;
        }
        
        Invoke(nameof(AttackTriggerCreat), 0.15f);//攻击触发器

        //攻击后延迟0.55秒
        Invoke(nameof(AttackEnd), 0.5f);
        Invoke(nameof(AttackAnimEnd), 0.6f);
        Invoke(nameof(AttackComboReset), 1f);
    }

    public void AttackEnd()
    {
        isAttack = false;
    }

    public void AttackAnimEnd()
    {
        animator.SetTrigger("AttackEnd");
    }

    public void AttackComboReset()
    {
        combo = 1;
    }
    
    public void AttackTriggerCreat()
    {
        //
        if (attackRightPos == null|| attackLeftPos == null)
        {
            Debug.LogWarning("攻击位置为空");
            return;
        }
        au.PlayOneShot(attackClip);//播放攻击音效
        if (!sr.flipX)
        {
            Instantiate(attackTrigger, attackRightPos.position, Quaternion.identity);
        }
        else
        {
            Instantiate(attackTrigger, attackLeftPos.position, Quaternion.identity);
        }
    }

}
//实现玩家空格键按下检测与调试输出